Thread: undefined reference to `getnumber' collect2.exe: error: ld returned 1 exit status

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    129

    undefined reference to `getnumber' collect2.exe: error: ld returned 1 exit status


    Hi All,

    How do I create a project using multiple source files?

    I understand the concept of including header files but I don't really know how to combine other source files.

    Here is what I tried

    main.c
    Code:
    #include<stdio.h>
    #include"other.h"
     
    int main (void)
    {
        printf("%d\n", getnumber());
     
        return 0;
    }


    other.h

    Code:
    #include "other.h"
     
    int getfavoritenumber(void)
    {
        return 3;
    }


    other.c
    Code:
    #ifndef _OTHER_H_
    #define _OTHER_H_
     
    int getnumber(void);
     
    #endif


    compiler gives error

    undefined reference to `getnumber'
    collect2.exe: error: ld returned 1 exit status

    What's wrong with files ?

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Well... for a start you haven't implemented getnumber() in other.c (assuming that you have just got the filenames back to front on here but they're right on your computer). You've implemented a function called getfavouritenumber() but not getnumber()

  3. #3
    Registered User
    Join Date
    May 2017
    Posts
    129
    I have replaced this file

    other.h
    Code:
    #include "other.h"
    int getnumber(void)
    {
        return 3;
    }
    error is same

  4. #4
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Ok, I think you have other.h and other.c mixed up.

    other.c should contain

    Code:
    #include "other.h"
    int getnumber(void)
    {
        return 3;
    }
    other.h should contain

    Code:
    #ifndef _OTHER_H_
    #define _OTHER_H_
      
    int getnumber(void);
      
    #endif
    i.e. the implementation of the function goes into the .c file (normally, and in this case certainly)

    Then compile with something like gcc -Wall main.c other.c (depending on what compiler you're using)

  5. #5
    Registered User
    Join Date
    May 2017
    Posts
    129
    Quote Originally Posted by Hodor View Post
    Ok, I think you have other.h and other.c mixed up.



    i.e. the implementation of the function goes into the .c file (normally, and in this case certainly)

    Then compile with something like gcc -Wall main.c other.c (depending on what compiler you're using)
    Here is complete file with path

    C:\Users\Vij\Desktop\other.c
    Code:
    #include "other.h"
    int getnumber(void)
    {
       return 3;
    }

    C:\Users\Vij\Desktop\other.h

    Code:
    #ifndef _OTHER_H_
    #define _OTHER_H_
      
    int getnumber(void);
      
    #endif
    C:\Users\Vij\Desktop\main.c

    Code:
    #include<stdio.h>
    #include"other.h"
     
    int main (void)
    {
        printf("%d\n", getnumber());
     
        return 0;
    }

    Then compile with something like gcc -Wall main.c other.c

    C:\Users\Vij\AppData\Local\Temp\cc8OxdX2.o:main.c.text+0xf): undefined reference to `getnumber'
    collect2.exe: error: ld returned 1 exit status

  6. #6
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    You're compiling with gcc?

    Code:
    $ pwd
    /home/HODOR/tmp/abhi
    
    $ ls
    main.c  other.c  other.h
    
    -------- main.c
    #include<stdio.h>
    #include"other.h"
      
    int main (void)
    {
        printf("%d\n", getnumber());
      
        return 0;
    }
    --------
    
    -------- other.c
    #include "other.h"
    int getnumber(void)
    {
       return 3;
    }
    --------
    
    -------- other.h
    #ifndef _OTHER_H_
    #define _OTHER_H_
       
    int getnumber(void);
       
    #endif
    --------
    
    $ gcc -Wall main.c other.c
    $
    (i.e. compiled with no warnings or errors)
    
    --------
    Output of program:
    
    3
    Last edited by Hodor; 12-06-2019 at 03:43 AM.

  7. #7
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Also, based on your path names you're using Windows. I know mingw and forks, and cygwin are popular but if you just need to compiler programs and not necessarily create Windows .exe files then I find that Windows Subsystem for Linux (WSL) is pretty good. The downsides are you need Windows 10 and you'll need to learn a few linux commands (but not many to get started). So, if you have Windows 10 and don't need Windows executables (i.e. just being able to compile and run programs is enough) then personally I'd use WSL instead of mingw or cygwin. The upside is that you get to learn Linux and don't have to dualboot etc etc. At least read about WSL if you have Windows 10.

    Edit: I'm not suggesting that mingw etc and cygwin are terrible (they're not and they serve the community well); I'm just suggesting that *I* would use WSL instead of those if I had a choice

  8. #8
    Registered User
    Join Date
    May 2017
    Posts
    129
    Quote Originally Posted by Hodor View Post

    Edit: I'm not suggesting that mingw etc and cygwin are terrible (they're not and they serve the community well); I'm just suggesting that *I* would use WSL instead of those if I had a choice
    Thank! Hodor

    Finally I got working. yes I am working on windows 10

    gcc -o main main.c other.c

    C:\Users\Vij\Desktop>main
    3

  9. #9
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by abhi143 View Post
    Thank! Hodor

    Finally I got working. yes I am working on windows 10

    gcc -o main main.c other.c

    C:\Users\Vij\Desktop>main
    3
    Great

    The -o is just to give the output file a name (other than a.out) so maybe that's a mingw requirement (it's not necessary on Linux). I'd definitely add -Wall to your command line, though, because you want compiler warnings turned on.

    gcc -Wall -o main main.c other.c

  10. #10
    Registered User
    Join Date
    May 2017
    Posts
    129
    Quote Originally Posted by Hodor View Post
    Great

    The -o is just to give the output file a name (other than a.out) so maybe that's a mingw requirement (it's not necessary on Linux). I'd definitely add -Wall to your command line, though, because you want compiler warnings turned on.

    gcc -Wall -o main main.c other.c
    gcc -Wall -o main main.c other.c

    yes this command also work on windows

  11. #11
    Registered User
    Join Date
    May 2017
    Posts
    129
    Quote Originally Posted by Hodor View Post
    Great

    The -o is just to give the output file a name (other than a.out) so maybe that's a mingw requirement (it's not necessary on Linux). I'd definitely add -Wall to your command line, though, because you want
    Hi Hodor

    I am struggling to compile code. Can you help me to compile code given in this link GitHub - EmbeddedApprentice/TaskTurner: A first, very short beginning for the TaskTurner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 09-23-2016, 01:40 PM
  2. collect2.exe: error: ld returned 1 exit status
    By umutefiloglu in forum C Programming
    Replies: 4
    Last Post: 05-11-2014, 09:37 AM
  3. collect2: ld returned 1 exit status error??
    By blindchicken11 in forum C Programming
    Replies: 11
    Last Post: 11-07-2011, 08:38 PM
  4. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  5. ERROR collect2: ld returned 1 exit status
    By meili100 in forum C++ Programming
    Replies: 13
    Last Post: 12-04-2007, 12:20 PM

Tags for this Thread